<?php
namespace app\pay\controller;

use weixinpay\Weixinpay as WeixinpayClass;
use think\Controller;
use think\Request;
use think\Db;
class Weixinpay extends Controller
{

    /**
     * notify_url接收页面
     */
    public function notify(WeixinpayClass $wxpay)
    {
        // ↓↓↓下面的file_put_contents是用来简单查看异步发过来的数据 测试完可以删除；↓↓↓
        // 获取xml
//
//        $xml=file_get_contents('php://input', 'r');
//        //转成php数组 禁止引用外部xml实体
//        libxml_disable_entity_loader(true);
//        $data= json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA));
//        file_put_contents(ROOT_PATH .'/public/upload/notify.text', $data);
//        // ↑↑↑上面的file_put_contents是用来简单查看异步发过来的数据 测试完可以删除；↑↑↑
//        // 导入微信支付sdk
        $result=$wxpay->notify();
        if ($result) {
            $out_trade_no = $result['out_trade_no'];
            Db::name('course_order')->where('order_id',$out_trade_no)->update(['order_status'=>1]);
            $course = Db::name('course_order')->where('order_id',$out_trade_no)->find();
            $user_course = Db::name('user_courselist')
                ->where('user_id',$course['user_id'])
                ->where('is_delete',0)
                ->where('now_course_time',0)
                ->where('course_id',$course['course_type_id'])->find();
            if(!$user_course){
                Db::name('user_courselist')->insert([
                    'user_id'=>$course['user_id'],
                    'now_course_time'=>0,
                    'course_id'=>$course['course_type_id']
                ]);
            }
            // 验证成功 修改数据库的订单状态等 $result['out_trade_no']为订单id
        }
    }


    /**
     * 公众号支付 必须以get形式传递 out_trade_no 参数
     */
    public function pay(WeixinpayClass $wxpay)
    {
        $params = Request::instance()->param();
        if(isset($params['out_trade_no'])&&isset($params['open_id'])&&isset($params['return_url'])){
            $order_id = $params['out_trade_no'];
            $order = Db::name('course_order')->where('order_id',$order_id)->find();
            if($order){
                $params['total_fee'] =  intval(floatval($order['order_price'])*100);
                $params['product_id'] = $order['course_type_id'];
                //获取jssdk需要用到的数据
                $data = $wxpay->getParameters($params);
                $this->assign(['data'=>json_encode($data),'return_url'=>$params['return_url']]);
                //将数据分配到前台页面
                return $this->fetch();
            }
            else{
                echo '<span style="font-size: 4rem">订单号不存在！</span>';
                exit;
            }
        }
        else{
            echo '<span style="font-size: 4rem">参数缺失！</span>';
            exit;
        }

    }

    public function asynorderquery(WeixinpayClass $wxpay){
        $params = Request::instance()->param();
        if(isset($params['user_id'])&&isset($params['out_trade_no'])){
            $user = Db::name('user')->where('is_delete',0)->where('id',$params['user_id'])->find();
            if($user){
                $open_id = $user['open_id'];
                $out_trade_no = $params['out_trade_no'];
                $res = $wxpay->orderquery($out_trade_no);
                $user = Db::name('user')->where('open_id',$open_id)->find();
                $orderexit = Db::name('course_order')->where('order_id',$out_trade_no)->find();
                if(!$user){
                    $data = [
                        'code'=>0,
                        'msg'=>'用户不存在',
                        'data'=>[]
                    ];
                    echo json_encode($data);
                    exit;
                }

                if(!$orderexit){
                    $data = [
                        'code'=>0,
                        'msg'=>'订单不存在',
                        'data'=>[]
                    ];
                    echo json_encode($data);
                    exit;
                }

                if(isset($res['trade_state'])&&$res['trade_state']=='SUCCESS'){
                    $order = Db::name('course_order')->where('order_id',$out_trade_no)->update(['order_status'=>1]);
                    $courseexit = Db::name('user_courselist')
                        ->where('is_delete',0)
                        ->where('user_id',$user['id'])
                        ->where('course_id',$orderexit['course_type_id'])
                        ->where('now_course_time',0)->find();
                    if($courseexit){
                        $data = [
                            'code'=>1,
                            'msg'=>'支付成功！且课程已存在！'
                        ];
                    }
                    else{
                        $course = Db::name('user_courselist')->insert([
                            'user_id'=>$user['id'],
                            'now_course_time'=>0,
                            'course_id'=>$orderexit['course_type_id']
                        ]);
                        if($order&&$course){
                            $data = [
                                'code'=>1,
                                'msg'=>'支付成功！'
                            ];
                        }
                    }
                }
                else{
                    $data = ['code'=>0,'msg'=>'订单不存在！'];
                }
            }
            else{
                $data = ['code'=>0,'msg'=>'用户不存在！'];
            }
        }
        else{
            $data = [
                'code'=>0,
                'msg'=>'参数缺失！',
                'data'=>[]
            ];
        }
        echo json_encode($data);
        exit;
    }
    public function orderquery(WeixinpayClass $wxpay){
        $params = Request::instance()->param();
        if(isset($params['out_trade_no'])&&isset($params['open_id'])){
            $out_trade_no = $params['out_trade_no'];
            $res = $wxpay->orderquery($out_trade_no);
            $user = Db::name('user')->where('open_id',$params['open_id'])->find();
            $orderexit = Db::name('course_order')->where('order_id',$out_trade_no)->find();
            if(!$user){
                $data = [
                    'code'=>0,
                    'msg'=>'用户不存在',
                    'data'=>[]
                ];
                echo json_encode($data);
                exit;
            }

            if(!$orderexit){
                $data = [
                    'code'=>0,
                    'msg'=>'订单不存在',
                    'data'=>[]
                ];
                echo json_encode($data);
                exit;
            }

            if($res['trade_state']=='SUCCESS'){
                $order = Db::name('course_order')->where('order_id',$out_trade_no)->update(['order_status'=>1]);
                $courseexit = Db::name('user_courselist')->where('user_id',$user['id'])->where('course_id',$orderexit['course_type_id'])
                ->where('now_course_time',0)->find();
                if($courseexit){
                    $data = [
                        'code'=>1,
                        'msg'=>'支付成功！且课程已存在！'
                    ];
                }
                else{
                    $course = Db::name('user_courselist')->insert([
                        'user_id'=>$user['id'],
                        'now_course_time'=>0,
                        'course_id'=>$orderexit['course_type_id']
                    ]);
                    if($order&&$course){
                        $data = [
                            'code'=>1,
                            'msg'=>'支付成功！'
                        ];
                    }
                }
            }
        }
        else{
            $data = [
                'code'=>0,
                'msg'=>'参数缺失！',
                'data'=>[]
            ];
        }
        echo json_encode($data);
        exit;
    }
    /**
     * 微信二维码支付
     * body(产品描述)、total_fee(订单金额)、out_trade_no(订单号)、product_id(产品id)
     */
    public function qr_pay()
    {
        // 虚拟的订单号 请根据实际业务更改
        $time = time();
        $order = [
            'body'=>'test',
            'total_fee'=>1,
            'out_trade_no'=>strval($time),
            'product_id'=>1
        ];
        weixinpay($order);
    }

}
